package com.FH.applyForm.officeServies.carManage;

import com.FH.util.AccessDB;
import com.FH.util.ResultDTO;
import com.FH.util.warnSrc;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import javax.ejb.Stateless;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2016/10/19.
 */
@Stateless
public class CarDAOImpl extends AccessDB implements CarDAO{
    /**
     * 查询车辆多类别信息
     * @param search
     * @param page
     * @param size
     * @return
     */
    @Override
    public ResultDTO getCarInfo(String search, String page, String size) {
        ResultDTO resultDTO = new ResultDTO();
        try {
            JSONObject json = new JSONObject(search);
            String existName = "0";

            String sqlexit = "select count(1) from carname where 1=1 ";

            String sql_w = "";//查询条件sql组合

            String sql_page = "";//分页sql组合
            Integer page_ok = Integer.parseInt(page);
            Integer size_ok = Integer.parseInt(size);
            if (page.equals("1")) {
                sql_page = " limit 0," + size + "";
            } else {
                sql_page = " limit " + ((page_ok - 1) * size_ok) + "," + size + "";
            }
            if (json.has("car_manage_id")) {
                sql_w += " AND car_manage_id = '" + json.getString("car_manage_id") + "'";
            }
            String sql_ok = sqlexit + sql_w;//组合好的sql语句
            existName = getReportingToOne(sql_ok);

            List<String[]> seresult = new ArrayList<String[]>();

            List<CarShowDTO> result = new ArrayList<CarShowDTO>();
            if (!existName.equals("0")) {
                String sql = "select id,member_id,name,car_manage_id from carname  where 1=1" + sql_w +  sql_page;
                seresult = this.getReportingTo(sql);

                //构建返回对象
                for (int i = 0; i < seresult.size(); i++) {

                    CarShowDTO carShowDTO = new CarShowDTO();
                    carShowDTO.setId(seresult.get(i)[1]);
                    carShowDTO.setMember_id(seresult.get(i)[2]);
                    carShowDTO.setName(seresult.get(i)[3]);
                    carShowDTO.setCar_manage_id(seresult.get(i)[4]);

                    result.add(carShowDTO);
                }
            }
            if (seresult.size() != 0) {
                resultDTO.setStatus("90000");
                resultDTO.setDescritpion(warnSrc.get_success);
            } else {
                resultDTO.setStatus("90001");
                resultDTO.setDescritpion(warnSrc.get_fail);
            }
            resultDTO.setStr(result.size() + "");
            resultDTO.setValues(result);

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return resultDTO;

    }

    /**
     * 添加车辆信息多类别
     * @param carPullDTO
     * @return
     */
    @Override
    public ResultDTO addCarInfo(CarPullDTO carPullDTO) {
        int resultls=0;
        ResultDTO resultDTO=new ResultDTO();

        String uuid= UUID.randomUUID().toString().trim().replaceAll("-", "");


        String Orgrole_insert="";
        Orgrole_insert="insert into carname (id,member_id,name,car_manage_id)" +
                "values('"+uuid+"','"+carPullDTO.getMember_id()+"','"+carPullDTO.getName()+"','"+carPullDTO.getCar_manage_id()+"')";

        resultls=addReportingTo(Orgrole_insert);
        if(resultls!=0){
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.add_success);
        }else{
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.add_fail);
        }
        return resultDTO;
    }

    /**
     * 修改多类别信息
     * @param carPullDTO
     * @return
     */
    @Override
    public ResultDTO updateCarInfo(CarPullDTO carPullDTO) {
        ResultDTO resultDTO = new ResultDTO();
        String QUERY = "";
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //更新sql语句
        QUERY += "update carname set ";
        if(null!=carPullDTO.getMember_id()&&!"".equals(carPullDTO.getMember_id())){
            QUERY += " member_id = '" + carPullDTO.getMember_id() + "'";
        }

        if(null!=carPullDTO.getName()&&!"".equals(carPullDTO.getName())){
            QUERY += ",name ='" + carPullDTO.getName() + "'";
        }

        if(null!=carPullDTO.getCar_manage_id()&&!"".equals(carPullDTO.getCar_manage_id())){
            QUERY += ",car_manage_id ='" + carPullDTO.getCar_manage_id() + "'";
        }

        QUERY += " where id = '" + carPullDTO.getId() + "'";


        int result = this.addReportingTo(QUERY);

        // 判断SQl是否执行成功
        if (result != 0) {
            resultDTO.setStatus("90000");//
            resultDTO.setDescritpion(warnSrc.update_success);
        } else {
            resultDTO.setStatus("90001");//
            resultDTO.setDescritpion(warnSrc.update_fail);
        }
        return resultDTO;
    }


    /**
     * 删除车辆类别被信息
     * @param carPullDTO
     * @return
     */
    @Override
    public ResultDTO deleteCarInfo(CarPullDTO carPullDTO) {
        String sql="select count(1) from carname WHERE car_manage_id='"+carPullDTO.getCar_manage_id()+"'";
        String existadvance= getReportingToOne(sql);

        int result = 0;

        ResultDTO resultDTO = new ResultDTO();
        // 删除SQL语句
        if(!existadvance.equals("0")){
            String QUERY = "";
            QUERY += "delete from carname  where car_manage_id='"+carPullDTO.getCar_manage_id()+"'";

            result = this.addReportingTo(QUERY);
        }

        // 判断SQl是否执行成功
        if (result!= 0) {

            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.delete_success);
        } else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.delete_fail);
        }
        return resultDTO;
    }
}
